package ms.db.mongodb;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDbFactory;
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableAutoConfiguration
public class MongodbConfiguration {

    private static final Logger LOGGER = LoggerFactory.getLogger(MongodbConfiguration.class);
    @Autowired
    MongoReplicaSetProperties mongoReplicaSetProperties;


    @Bean
    @ConfigurationProperties(prefix = "spring.data.mongodb.custom")
    MongoReplicaSetProperties mongoReplicaSetProperties() {
        return new MongoReplicaSetProperties();
    }


    @Bean
    MongoDbFactory mongoDbFactory() {
        MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
        builder.connectionsPerHost(mongoReplicaSetProperties.getConnectionsPerHost());
        builder.minConnectionsPerHost(mongoReplicaSetProperties.getMinConnectionsPerHost());
        if (mongoReplicaSetProperties.getReplicaSet() != null) {
            builder.requiredReplicaSetName(mongoReplicaSetProperties.getReplicaSet());
        }
        MongoClientOptions mongoClientOptions = builder.build();

        // MongoDB地址列表
        List<ServerAddress> serverAddresses = new ArrayList<>();
        for (String host : mongoReplicaSetProperties.getHostMap().keySet()) {
            Integer port = mongoReplicaSetProperties.getHostMap().get(host);
            ServerAddress serverAddress = new ServerAddress(host, port);
            serverAddresses.add(serverAddress);
        }
        LOGGER.info("mongodb hosts:" + serverAddresses.toString());

        List<MongoCredential> mongoCredentialList = new ArrayList<>();
        if (mongoReplicaSetProperties.getUsername() != null) {
            mongoCredentialList.add(MongoCredential.createScramSha1Credential(
                    mongoReplicaSetProperties.getUsername(),
                    mongoReplicaSetProperties.getAuthenticationDatabase() != null ? mongoReplicaSetProperties.getAuthenticationDatabase() : mongoReplicaSetProperties.getDatabase(),
                    mongoReplicaSetProperties.getPassword().toCharArray()));
        }
        LOGGER.info("mongo credentials:" + mongoCredentialList.toString());

        //创建客户端和Factory
        MongoClient mongoClient = new MongoClient(serverAddresses, mongoCredentialList, mongoClientOptions);
        MongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, mongoReplicaSetProperties.getDatabase());

        return mongoDbFactory;
    }

}
